package com.itheima.controller;

import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.resources.MessageConstant;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.UploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController   //  @Controller +@ResponseBody
@Api(tags = "套餐管理")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("setmeal/add")
    public Result add(@RequestBody SetmealDTO setmealDTO){
        try {
            setmealService.saveSetemal(setmealDTO);
            return  new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

     @PostMapping("setmeal/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
           PageResult pageResult =  setmealService.findPage(queryPageBean);
            return  new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }


    @PostMapping("setmeal/upload")
    public Result upload(@RequestParam("imgFile")MultipartFile file){
        try {
             //  调用阿里云 oss  sdk   完成  图片上传
            String originalFilename = file.getOriginalFilename();
            String uuidfilename = UploadUtils.generateRandonFileName(originalFilename);
            AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(),uuidfilename);
            //   服务端生成图片uuid 名称 回送给 前端

            //  图片   保存在redis  上  以 set 形式
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES,uuidfilename);

            return  new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,uuidfilename);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

//    @GetMapping("checkitem/findCheckItemById/{id}")
//    public Result findCheckItemById(@PathVariable("id") int id){
//        try {
//            CheckItem checkItem = checkItemService.getById(id);
//            return  new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS,checkItem);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return  new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
//        }
//    }
//
//    @DeleteMapping("checkitem/deleteCheckItemById/{id}")
//    public Result  deleteCheckItemById(@PathVariable("id") int id){
//        try {
//             CheckItem checkItem = new CheckItem();
//             checkItem.setIs_delete(1);  //  逻辑删除
//             checkItem.setId(id);
//             checkItemService.updateById(checkItem);
//            return  new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return  new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
//        }
//    }

}
